If you don’t provide an access specifier for a class member, the member is given package access by default, meaning that any method in the same package may access the member.
~
A superclass can inherit from another class.
Object
ClassAll Java classes are directly or indirectly derived from a class named Object
in the java.lang
package.
public class MyClass extends Object
).Thus, every class inherits the Object
class’s members (which we often override):
toString()
: Returns string containing class name and hash of memory address.equals()
: Accepts address of an object and returns true if it is the same address as the calling object’s address.Polymorphism: Ability to take many forms.
A superclass reference variable can reference objects of a subclasses.
; GradedActivity exam
= new GradedActivity(); exam
FinalExam
object= new FinalExam(50,7); GradedActivity exam
When a superclass variable references a subclass object that has overridden a method in the superclass, the subclass’s version will be run when it’s called.
Java performs dynamic binding or late binding when a variable contains a polymorphic reference.
instanceof
Operatorinstanceof
can be used to determine whether an object is an instance of a particular class.
= new FinalExam(20,2);
FinalExam exam if (exam instanceof GradedActivity)
// Does run
else
// Doesn't run